home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.cyberramp.net!news
- From: sinan@cyberramp.net (John Noland)
- Newsgroups: comp.os.msdos.programmer,comp.lang.c
- Subject: Re: open vs fopen?
- Date: 15 Feb 1996 00:31:27 GMT
- Organization: Prose Software
- Message-ID: <4ftusv$181@newshost.cyberramp.net>
- References: <uEYFxc9nX8WX083yn@mbnet.mb.ca> <4f8bev$6tr@hermes.louisville.edu> <2d3avbl60.alamito@marketgraph.xs4all.nl>
- NNTP-Posting-Host: ramp3-11.cyberramp.net
- X-Newsreader: WinVN 0.99.5
-
- natewild@mbnet.mb.ca (Nathan T. Wild) writes:
- >
- >In C, why would one use open and DOS int handles rather than fopen and
- >stdio file handles?
- >
- >The only reason I know of is UNIX portability. Whenever I run across a
- >unix program that uses the old-style open instead of fopen I usually
- >end up rewriting it anyway though; I've had so many problems with
- >the unix-style stuff I just rather would deal with the ANSI style.
- >
- >David
- >>>Is there some speed advantage or differnet functionality?
- >
- >fopen() is mostly buffered, open() is, I believe, not. open() also gives some
- >extra possible flags for sharing options. I use fopen() without problems.
- >Besides, it's ANSI, and I program multiple programs and sometimes share
- >modules that I'd have to check and redesign when switching from fopen to the
- >local open command.
-
- The open() function and its ilk are normally referred to as the "low-level"
- I/O package. fopen() is the "Buffered" or "Standard" I/O package. The
- strength of the low-level I/O functions is that they offer excellent control,
- particularly when used with binary files. If you have a special I/O need, you
- can use the low-level I/O routines to fashion the exact I/O package to fit
- your needs.
- The standard I/O package is one such creation. It is designed to provide fast
- buffered I/O, mostly for text situations. For a lot of applications, using the
- standard I/O package is simpler and more effective than using simple low-level
- output. The essential feature is its use of automatic buffering. Buffered
- I/O means reading and writing data in large chunks from a file to an array
- and back. Reading and writing data in large chunks greatly speeds up the I/O
- operations, while storing in an array allows access to the individual bytes.
- The advantages of this approach should be apparent.
- When fopen() is used, several things happen. The file, of course, is opened.
- Second, an external character array is created to act as a buffer. The
- <stdio.h> file has this buffer set to a size of 512 bytes. Third, a structure
- is created to contain information about the buffer and the file. When you
- call one of the functions from the standard I/O library, such as getc(),
- the buffer is filled with data from the file. When getc() reaches the end of
- the buffer, the buffer is automatically with the next block of data from the
- file. You need not even be aware of the buffer, and you can program as if the
- I/O functions worked directly with the file.
- Technically, in DOS, all I/O is buffered since DOS itseld uses buffers
- for disk I/O. This is a transparent function of the operating system and
- has nothing to do with I/O packages included with C.
-
- Hope this clarifies things.
-
- -John Noland
-
-